package com.acegear.horizon.domain.repository;

import com.acegear.horizon.domain.models.Club;
import com.acegear.horizon.domain.models.constraint.FeedType;
import com.acegear.horizon.domain.models.jpa.ClubFeed;
import com.acegear.horizon.domain.repository.jpa.ClubBaseRepository;
import com.acegear.horizon.domain.repository.jpa.ClubFeedRepository;
import com.acegear.horizon.domain.repository.jpa.ClubInfoRepository;
import com.acegear.horizon.domain.repository.jpa.ClubMemberRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.stream.Stream;

/**
 * Created by wangsike on 2016/12/20.
 */
@Component
public class ClubRepository {

    @Autowired
    private ClubBaseRepository clubBaseRepository;

    @Autowired
    private ClubInfoRepository clubInfoRepository;

    @Autowired
    private ClubMemberRepository clubMemberRepository;

    @Autowired
    private ClubFeedRepository clubFeedRepository;

    public Optional<Club> findByClubId(Long clubId) {
        return clubBaseRepository.findByClubId(clubId).map(Club::new);
    }

    /**
     * 添加一条feed
     * @return
     */
    public ClubFeed saveFeed(FeedType type, Long clubId, Long relateId) {
        Integer oldTopPosition = clubFeedRepository.findMaxPosition(clubId);
        if (oldTopPosition == null) {
            oldTopPosition = 0;
        }
        ClubFeed clubFeed = new ClubFeed();
        clubFeed.setFeedType(type);
        clubFeed.setClubId(clubId);
        clubFeed.setRelateId(relateId);
        clubFeed.setPosition(oldTopPosition + 1);
        clubFeed.setOldPositon(oldTopPosition + 1);
        return clubFeedRepository.save(clubFeed);
    }

    /**
     * 删除一条feed
     */
    public void deleteFeed(FeedType type, Long clubId, Long relateId) {
        clubFeedRepository.deleteByClubIdAndRelateIdAndFeedType(clubId, relateId, type);
    }

    /**
     * 置顶一条feed
     */
    public ClubFeed topFeed(Long clubId, Long relateId, FeedType feedType) {
        ClubFeed clubFeed = clubFeedRepository.findByClubIdAndRelateIdAndFeedType(clubId, relateId, feedType);
        if (clubFeed == null)
            return null;
        clubFeed.setTop(true);

        return clubFeedRepository.save(clubFeed);
    }

    /**
     * 取消置顶一条feed
     */
    public ClubFeed unTopFeed(Long clubId, Long relateId, FeedType feedType) {
        ClubFeed clubFeed = clubFeedRepository.findByClubIdAndRelateIdAndFeedType(clubId, relateId, feedType);
        if (clubFeed == null)
            return null;
        clubFeed.setTop(false);
        clubFeed.setPosition(clubFeed.getOldPositon());

        return clubFeedRepository.save(clubFeed);
    }

    public Stream<Club> query(boolean register, Pageable pageable) {
        return clubBaseRepository
                .findByRegister(register, pageable)
                .stream()
                .map(Club::new);
    }
}
